<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        @enter="handleQuery"
        label-width="65px"
      >
        <el-form-item label="IP" prop="nmapIp">
          <common-input
            v-model="queryParams.nmapIp"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="端口" prop="nmapPort">
          <common-input
            v-model="queryParams.nmapPort"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <!-- <el-form-item label="业务系统" prop="businessSystem">
          <common-input
            v-model="queryParams.businessSystem"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->

        <el-form-item label="服务名称" prop="serverName">
          <common-input
            v-model="queryParams.serverName"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="产品名称" prop="productName">
          <common-input
            v-model="queryParams.productName"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="关联资产" prop="status">
          <el-select v-model="queryParams.status">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="黑名单" prop="blackList">
          <el-select v-model="queryParams.blackList">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="是" :value="'1'"></el-option>
            <el-option label="否" :value="'0'"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发现时间">
          <el-date-picker
            v-model="date"
            type="datetimerange"
            @change="dateChange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
    </common-search>

    <el-row :gutter="10" class="mb8" slot="tools">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-user-solid"
          :disabled="ids.length < 1"
          @click="handleJoinBlacklist"
          v-hasPermi="['nmap:record:blackList']"
          >加入黑名单
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-user"
          :disabled="ids.length < 1"
          @click="handleRemoveBlacklist"
          v-hasPermi="['nmap:record:blackList']"
          >移除黑名单
        </el-button>
      </el-col>
    </el-row>

    <common-table
      v-loading="loading"
      slot="content"
      :data="databaseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="IP"
        align="left"
        prop="nmapIp"
        show-overflow-tooltip
      />

      <el-table-column
        label="端口"
        align="left"
        prop="nmapPort"
        show-overflow-tooltip
      />

      <!-- <el-table-column
        label="业务系统"
        align="left"
        prop="businessSystem"
        show-overflow-tooltip
      /> -->

      <el-table-column
        label="服务名称"
        align="left"
        prop="serverName"
        show-overflow-tooltip
      />
      <el-table-column
        label="产品名称"
        align="left"
        prop="productName"
        show-overflow-tooltip
      />
      <el-table-column
        label="发现时间"
        align="left"
        prop="createTime"
        width="180"
      />
      <el-table-column label="关联资产" align="left" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">是</el-tag>
          <el-tag v-if="scope.row.status === 0" type="danger">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="黑名单" align="left" prop="blackList" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.blackList === '1'" type="success">是</el-tag>
          <el-tag v-if="scope.row.blackList === '0'" type="danger">否</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left" width="150px">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-hasPermi="['nmap:record:relation']"
            :disabled="scope.row.status === 1"
            @click="relatedAssets(scope.row)"
            >关联资产
          </el-button>

          <el-button
            type="text"
            v-hasPermi="['nmap:record:blackList']"
            @click="handleJoinBlacklist(scope.row)"
            v-if="scope.row.blackList === '0'"
            >加入黑名单
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['nmap:record:blackList']"
            @click="handleRemoveBlacklist(scope.row)"
            v-if="scope.row.blackList === '1'"
            >移除黑名单
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <related-assets-add
      ref="relatedAssets"
      :isRelatedAssets="true"
      :businessCodeList="businessCodeList"
      @flushedList="handleList"
    ></related-assets-add>

  </common-list>
</template>

<script>
import { fetchList, blacklist } from "@/api/datascan/databaseManage/index";
import BaseList from "@/mixins/BaseList";
import RelatedAssetsAdd from "@/views/datascan/dbinfo/components/addDialog.vue";
import { queryBusinessList } from "@/api/datascan/dbinfo/info";
import { queryGroupList } from "@/api/datascan/dbgroup/group";
import { queryListByTypeCode } from "@/api/datascan/scanresults/column";

export default {
  mixins: [BaseList],
  components: {
    RelatedAssetsAdd,
  },
  data() {
    return {
      ids: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nmapIp: null,
        nmapPort: null,
        businessSystem: null,
        productName: null,
        status: 0,
        blackList: "0",
        systemName: "DATASCAN",
      },
      total: 0,
      databaseList: [],
      loading: false,
      detailShow: false,
      detailId: null,
      date: [],
      dbTypeList: [],
      dbGroupList: [],
      businessCodeList: [],
      deptListParams: [],
    };
  },
  created() {
    this.getList();

    this.getDataSourceTypeList(); //数据库类型
    this.getGroupList(); //数据库组
    this.queryBusinessCodeList(); //业务系统
  },

  methods: {
    getList() {
      this.loading = true;
      fetchList(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.databaseList = res.rows;

            this.total = res.total;
          } else {
            this.databaseList = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .catch(() => {
          this.databaseList = [];
          this.total = 0;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 关联资产
    relatedAssets(row) {
      this.$refs.relatedAssets.show(row || {});
    },

    //加入 黑名单
    handleJoinBlacklist(row) {
      let includeNoBlacklist = this.databaseList.find((v) => {
        return this.ids.includes(v.id) && v.blackList === "1";
      });
      if (includeNoBlacklist) {
        this.$GlobalErrorConfirm("操作失败", "包含黑名单的数据");
        return;
      }
      const ids = row.id || this.ids.join(",");
      this.blacklistFun(ids, "1", "加入");
    },

    //移除黑名单
    handleRemoveBlacklist(row) {
      let includeBlacklist = this.databaseList.find((v) => {
        return this.ids.includes(v.id) && v.blackList === "0";
      });
      if (includeBlacklist) {
        this.$GlobalErrorConfirm("操作失败", "包含非黑名单的数据");
        return;
      }

      const ids = row.id || this.ids.join(",");
      this.blacklistFun(ids, "0", "移除");
    },

    //加入/移除 黑名单公共处理方法
    blacklistFun(ids, status, text) {
      this.$confirm("是否确认" + text + "黑名单?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          blacklist({ ids: ids, blackList: status }).then((res) => {
            if (res && res.code == 200) {
              this.$GlobalSuccessMessage("保存成功");
              if (
                this.queryParams.pageNum > 1 &&
                ids.split(",").length === this.databaseList.length
              ) {
                this.queryParams.pageNum -= 1;
              }
              this.getList();
            } else {
              this.$GlobalErrorConfirm("保存失败", res.msg);
            }
          });
        })
        .catch(() => {});
    },

    dateChange() {
      let endTime = null,
        beginTime = null;
      if (this.date && this.date.length > 0) {
        beginTime = this.date[0];
        endTime = this.date[1];
      }
      this.queryParams.beginTime = beginTime || null;
      this.queryParams.endTime = endTime || null;
    },

    handleList() {
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      this.queryParams.status = null;
      this.queryParams.blackList = null;
      this.date = [];
    },

    //数据库类型
    getDataSourceTypeList() {
      queryListByTypeCode().then((res) => {
        if (res && res.code === 200) {
          this.dbTypeList = res.data.dataList.reduce((r, c) => {
            r.push({
              label: c.name,
              value: c.value,
            });
            return r;
          }, []);
        } else {
          this.dbTypeList = [];
        }
      });
    },

    //数据库组列表
    getGroupList() {
      queryGroupList({}).then((res) => {
        this.dbGroupList = res.data.dataList.reduce((r, c) => {
          r.push({
            label: c.groupName,
            value: c.id,
            deptName: c.deptName,
            deptId: c.deptId,
          });
          return r;
        }, []);
      });
    },

    //获取业务系统列表
    queryBusinessCodeList() {
      queryBusinessList().then((res) => {
        if (res && res.code === 200) {
          this.businessCodeList = res.data;
        } else {
          this.businessCodeList = [];
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
